﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Handrise.BasicModal.Serialization;

namespace Handrise.CDCable.Business
{
    /// <summary>
    /// 巡视信息服务
    /// </summary>
    public class PatrolInfoService
    {
        private ServerExchange serverExchange = new ServerExchange();

        /// <summary>
        /// 上传巡视记录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="patrolLogsBuffer">巡视日志实体列表序列化流</param>
        /// <param name="patrolDetailsBuffer">巡视详情实体列表序列化流</param>
        /// <returns></returns>
        public bool UploadPatrolInfo(string userName, byte[] patrolLogsBuffer, byte[] patrolDetailsBuffer)
        {
            CompactFormatterPlus cfp = new CompactFormatterPlus();
            List<PatrolLog> patrolLogs = (List<PatrolLog>)cfp.Deserialize(patrolLogsBuffer);
            List<PatrolDetail> patrolDetails = (List<PatrolDetail>)cfp.Deserialize(patrolDetailsBuffer);

            //处理巡视记录详情，需要把巡视详情中的数据进行整合
            List<PatrolsRecordDetail> patrolsRecordDetails = new List<PatrolsRecordDetail>();
            foreach (PatrolDetail patrolDetail in patrolDetails)
            {
                PatrolsRecordDetail patrolsRecordDetail = new PatrolsRecordDetail();
                patrolsRecordDetail.FID = patrolDetail.PatrolDetailID;
                if (string.IsNullOrEmpty(patrolDetail.DeviceName))
                    patrolsRecordDetail.FCONTENT = patrolDetail.Content;
                else
                    patrolsRecordDetail.FCONTENT = patrolDetail.DeviceName + "：" + patrolDetail.Content;
                patrolsRecordDetail.FPATROLSKEY = patrolDetail.PatrolItemName;
                patrolsRecordDetail.FPATROLSTYPE = patrolDetail.WorkType;
                patrolsRecordDetail.PatrolLogID = patrolDetail.PlanLogID;
                patrolsRecordDetail.VERSION = "0";
                switch (patrolDetail.ParentDeviceType)
                {
                    case "电缆线路":
                        patrolsRecordDetail.FLINENAMES = patrolDetail.ParentDeviceName;
                        patrolsRecordDetail.FSUMLINELENGTH = patrolDetail.ParentDeviceLength;
                        break;
                    case "电力隧道":
                        patrolsRecordDetail.FTUNNELNAMES = patrolDetail.ParentDeviceName;
                        patrolsRecordDetail.FSUMTUNNELLENGTH = patrolDetail.ParentDeviceLength;
                        break;
                    default:
                        break;
                }
                patrolsRecordDetails.Add(patrolsRecordDetail);
            }

            string patrolLogsXML = CommonFunction.GetXMLFromEnitys<PatrolLog>(patrolLogs, typeof(PatrolLog));
            string patrolDetailsXML = CommonFunction.GetXMLFromEnitys<PatrolsRecordDetail>(patrolsRecordDetails, typeof(PatrolsRecordDetail));

            string receiveData = serverExchange.UploadPatrolInfo(userName, patrolLogsXML, patrolDetailsXML);

            return CommonFunction.ReturnDataIsOk(receiveData); ;
        }


    }
}
